﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Linq;
using FreeGoo.GameEngine;

namespace FreeGoo.LevelClasses
{
    public class CampaignHandler
    {
        public CampaignHandler()
        {
            Campaigns = new ObservableCollection<Campaign>();
        }

        public CampaignHandler(Engine engine) : this()
        {
            Engine = engine;
        }

        public ObservableCollection<Campaign> Campaigns { get; private set; }
        public Engine Engine { get; set; }
        public string BasePath
        {
            set
            {
                Environment.CurrentDirectory = value;
            }
        }

        public void LoadFromFile(string fileName)
        {
            Campaigns.Clear();
            Uri baseUri = new Uri(Path.GetFullPath(fileName));
            XElement root = XDocument.Load(fileName).Root;
            foreach (XElement element in root.Elements("Campaign"))
            {
                Uri uri = new Uri(baseUri, element.Attribute("FileName").Value);
                Campaigns.Add(new Campaign(this, uri.AbsolutePath));
            }
        }
    }
}
